package com.pmcc.utils;

import com.pmcc.core.config.security.JwtUser;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.security.core.userdetails.UserDetails;

import java.util.ArrayList;
import java.util.List;

/**
 * 系统登录人信息
 */
public class OnlineUser {


    /**
     *
     * @return 当前登陆人姓名
     */
    public static String getName(){

        String name="";
        JwtUser currentAccount = getCurrentAccount();
        if(currentAccount!=null){
            name=currentAccount.getName();
        }
        return name;
    }

    private static JwtUser getCurrentAccount() {
        try {
            JwtUser jwtUser=null;
            UserDetails userDetails = (UserDetails) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
            if(userDetails!=null){
                if(userDetails instanceof JwtUser){
                    jwtUser=(JwtUser)userDetails;
                }
            }
            return jwtUser;
        }catch (Exception e){
            return null;
        }
    }

    /**
     *
     * @return 当前登陆人主键
     */
    public static String getEmployeeId(){

        String id="";
        JwtUser currentAccount = getCurrentAccount();
        if(currentAccount!=null){
            id=currentAccount.getId();
        }
        return id;
    }


}

